package com.uchengwang.ui.cm_ui.ucity.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.rychat.bean.RyUserBean;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/4/28.
 */

public class ApplyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private boolean isShow;
    private List<UserBean> list = new ArrayList<>();
    private Handler handler;
    private List<RyUserBean> members;
    /**
     * 多选操作框 只有当该消息是该查看人发布时才显示
     * */
    public ApplyAdapter (Context context , boolean isShow){
        this.context = context;
        this.isShow = isShow;
    }

    public void setData(List<UserBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<UserBean> getData(){
        return list;

    }

    /**
     * 将已经是群成员的用户 复选框过滤掉
     * */
    public void setMembers(List<RyUserBean> members){
        this.members = members;
        notifyDataSetChanged();
    }

    public void setHandle(Handler handle){
        this.handler = handle;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.apply_item, parent, false);
        return new ApplyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, final int position) {
        final ApplyViewHolder holder = (ApplyViewHolder) h;

        final UserBean bean = list.get(position);

        if (isShow){
            if (isMember(bean.getId())){
                holder.apply_checkBox.setVisibility(View.GONE);
            }else {
                holder.apply_ln.setVisibility(View.VISIBLE);
                holder.apply_checkBox.setVisibility(View.VISIBLE);
            }
        }else {
            holder.apply_checkBox.setVisibility(View.GONE);
        }

        String headUrl = bean.getUserAvatar();
        if (!TextUtils.isEmpty(headUrl)) {
            ImageManager.getInstance().loadImage(context, holder.apply_image, headUrl, ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.apply_image, R.drawable.no_head, ImageManager.ImageType.CIRCLE);

        }
        String remark = bean.getRemarkName();
        if (TextUtils.isEmpty(remark)){
            holder.apply_name.setText(bean.getNickName());
        }else {
            holder.apply_name.setText(remark);
        }

        String uniName = bean.getUniName();
        holder.apply_info.setText(TextUtils.isEmpty(uniName) ? "暂无相关信息" : uniName);

//        holder.apply_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                bean.setCheck(b);
//                if (handler != null){
//                    Message msg = handler.obtainMessage(0);
//                    msg.obj = b;
//                    handler.sendMessage(msg);
//
//                }
//            }
//        });

        holder.apply_checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isCheck = bean.isCheck();
                holder.apply_checkBox.setChecked(!isCheck);
                bean.setCheck(!isCheck);
                if (handler != null){
                    Message msg = handler.obtainMessage(0);
                    msg.obj = !isCheck;
                    handler.sendMessage(msg);

                }
            }
        });

        holder.apply_checkBox.setChecked(bean.isCheck());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UCommonUtil.getUtil().toUserActivity((Activity) context, bean.getId());

            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ApplyViewHolder extends RecyclerView.ViewHolder{
        private View itemView;
        private ImageView apply_image;
        private TextView apply_name, apply_info;
        private AppCompatCheckBox apply_checkBox;
        private LinearLayout apply_ln;


        public ApplyViewHolder(View view) {
            super(view);
            AutoUtils.autoSize(view);

            itemView = view;

            apply_image = (ImageView) view.findViewById(R.id.apply_image);
            apply_name = (TextView) view.findViewById(R.id.apply_name);
            apply_info = (TextView) view.findViewById(R.id.apply_info);
            apply_ln = (LinearLayout) view.findViewById(R.id.apply_ln);
            apply_checkBox = (AppCompatCheckBox) view.findViewById(R.id.apply_checkBox);
            apply_checkBox.setClickable(false);
        }
    }

    /**
     *判断此人是否已是成员
     */
    public boolean isMember(long uid){
        if (members != null && members.size() >0){
            int count = members.size();
            for (int i=0; i < count; i++){
                if (uid == members.get(i).getMemberUid() || uid ==UCommonUtil.getUtil().getId()){
                    return true;
                }
            }
            return false;
        }else {
            if (uid ==UCommonUtil.getUtil().getId()){
                return true;
            }
            return false;
        }
    }
}
